using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public partial class asp_pages_admin_ProductSaleOff : System.Web.UI.Page
{
    private const int pageSize = 20; 
    ProductDAO daoProduct = new ProductDAO();
 
    int totals;
    int currentPage;
    int mode = 0;
    string p;
    protected void Page_Load(object sender, EventArgs e)
    {
        p = Request["page"];
        totals = 0;
        currentPage = GetPageNumber(p);
        if (!IsPostBack)
        {
            ProductCategoryDAO daoCate = new ProductCategoryDAO();
            IList<ProductCategory> cates = daoCate.GetAll();
            foreach (ProductCategory cate in cates)
            {
                cmbType.Items.Add(new ListItem(cate.Name, cate.ID.ToString()));
            }
            int categoryID = int.Parse(cmbType.SelectedValue);
            LoadData(pageSize, currentPage, categoryID, ref totals);
            // hien thi lien ket phan trang
            lblPaging.Text = GetPageGrid(totals, pageSize, currentPage);
        }

    }
    public void LoadData(int pageSize, int currentPage, int categoryID, ref int totalRecords)
    {
        IList<Product> list = daoProduct.GetProductByCategory(currentPage, pageSize, categoryID, ref totalRecords);
        grvProducts.DataSource = list;
        grvProducts.DataBind();

    }
    private bool IsNumeric(string strToCheck)
    {
        if (strToCheck == null || strToCheck == "")
            return false;
        return Regex.IsMatch(strToCheck, "^\\d+$");
    }
    private int GetPageNumber(string p)
    {
        int result;
        if (p == "")
            result = 1;
        if (!IsNumeric(p))
            result = 1;
        else
            result = int.Parse(p);
        return result;
    }

    private string GetPageGrid(int totalRecords, int iPageSize, int iCurrentPageIndex)
    {
        // bao nhieu chi so phan trang
        const int QUICK_PAGE = 5;
        int i = 0;
        //string str = null;
        string str = "Page:";
        int iCurrentPage = 0;
        int iPageCount = 0;
        int iSpace = 0;
        //int iQuickPage = 0;
        int iStart = 0;
        int iEnd = 0;

        iPageCount = (int)(totalRecords / iPageSize);
        if (totalRecords % iPageSize > 0)
        {
            iPageCount += 1;
        }

        iCurrentPage = iCurrentPageIndex;
        //iQuickPage = QUICK_PAGE;
        if (iCurrentPage % QUICK_PAGE > 0)
        {
            iSpace = (int)(iCurrentPage / QUICK_PAGE) + 1;
        }
        else
        {
            iSpace = (int)(iCurrentPage / QUICK_PAGE);
        }
        if (iPageCount < iSpace * QUICK_PAGE)
        {
            iEnd = (iSpace * QUICK_PAGE) - ((iSpace * QUICK_PAGE) - iPageCount);
        }
        else
        {
            iEnd = (iSpace * QUICK_PAGE);
        }

        if (iEnd < QUICK_PAGE)
        {
            iStart = 1;
        }
        else
        {
            iStart = (iEnd - QUICK_PAGE) + 1;
        }

        if (iCurrentPage > QUICK_PAGE)
        {
            if (iStart - 1 == iCurrentPage)
            {
                str = str + "...";
            }
            else
            {
                str += " " + "<a href = 'SaleOffSetting.aspx?page=" + (iStart - 1) + "'>" + "..." + "</a>";
            }
        }
        for (i = iStart; i <= iEnd; i++)
        {
            if (i == iCurrentPage)
            {
                str += " " + i;
            }
            else
            {
                str += " " + "<a href = 'SaleOffSetting.aspx?page=" + i + "'>" + i + "</a>";
            }
        }
        if (iCurrentPage < iPageCount & iEnd < iPageCount)
        {
            if (iCurrentPage == iEnd + 1)
            {
                str = " " + str + "...";
            }
            else
            {
                str += " " + "<a" + " href = 'SaleOffSetting.aspx?page=" + (iEnd + 1) + "'>" + "..." + "</a>";
            }
        }
        return str;
    }
    protected void grvProducts_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvProducts.EditIndex = e.NewEditIndex;
        int categoryID = int.Parse(cmbType.SelectedValue);
        LoadData(pageSize, currentPage, categoryID, ref totals);
        // hien thi lien ket phan trang
        lblPaging.Text = GetPageGrid(totals, pageSize, currentPage);
    }
    protected void grvProducts_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvProducts.EditIndex = -1;
        int categoryID = int.Parse(cmbType.SelectedValue);
        LoadData(pageSize, currentPage, categoryID, ref totals);
        // hien thi lien ket phan trang
        lblPaging.Text = GetPageGrid(totals, pageSize, currentPage);
    }
    protected void grvProducts_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = int.Parse(grvProducts.DataKeys[e.RowIndex].Value.ToString());
        string strSaleOff = ((TextBox)grvProducts.Rows[e.RowIndex].FindControl("txtSaleOff")).Text;
        float saleOff = 0;
        if (strSaleOff.Trim() != "") saleOff = float.Parse(strSaleOff);
        //daoPet.UpdateSaleOff(id, saleOff);
        grvProducts.EditIndex = -1;
        int categoryID = int.Parse(cmbType.SelectedValue);
        LoadData(pageSize, currentPage, categoryID, ref totals);
        // hien thi lien ket phan trang
        lblPaging.Text = GetPageGrid(totals, pageSize, currentPage);

    }
    protected void btnView_Click(object sender, EventArgs e)
    {
        int categoryID = int.Parse(cmbType.SelectedValue);
        LoadData(pageSize, currentPage, categoryID, ref totals); int cateID = int.Parse(cmbType.SelectedValue);
        lblPaging.Text = GetPageGrid(totals, pageSize, currentPage);
    }
    protected void grvProducts_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(grvProducts.DataKeys[e.RowIndex].Value.ToString());
        Product pro=new Product();
        pro.ID=id;
        if (daoProduct.DeleteProduct(pro)>0)
        {
            int categoryID = int.Parse(cmbType.SelectedValue);
            LoadData(pageSize, currentPage, categoryID, ref totals); int cateID = int.Parse(cmbType.SelectedValue);
            lblPaging.Text = GetPageGrid(totals, pageSize, currentPage);
        }

    }
}
